<?php if (! defined('BASEPATH')) exit('No direct script access allowed');

class Redirect extends CI_Controller {
	public function __construct() {
		parent::__construct();
        $this->load->model('author_model');
        $this->load->model('book_model');
	}


    public function index() {
        $this->load->library("Douban_api");
        $id = filter_input(INPUT_GET,'book_id',FILTER_VALIDATE_INT);
        $douban_api = Douban_api::getInstance();
        $book_data = $douban_api->getBooksById($id);
        $author_id = 0;
        $author = isset($book_data->author[0])&&!empty($book_data->author[0])?$book_data->author[0]:"";
        $author_intro = empty($book_data->author_intro)?"":$book_data->author_intro;
        $attr = array(
            "author"=>$author,
            "publisher"=>$book_data->publisher,
            "pubdate"=>$book_data->pubdate,
            "isbn"=>$book_data->isbn13,
            "binding"=>$book_data->binding,
            "pages"=>$book_data->pages,
        );//出版社,出版时间，isbn，装帧，开本，字数，版本+
  
       // dump($book_data);
        $author_info = $this->author_model->getAuthorByName($author);
        if(empty($author_info)){
            $author_data = array(
                'author' => $author,
                'author_intro' => $author_intro,
            );
            $author_id = $this->author_model->insertAuthor($author_data);
        }else{
            $author_id = $author_info[0]['id'];
        }
        
        $data = array(
            'name' => $book_data->title,
            'price' => floatval($book_data->price),
            'book_id' => $id,
            'image' => $book_data->images->large,
            'author_id' => $author_id ,
            'author_name' => $author,
            'parameter' => json_encode($attr),
            'summary' => $book_data->summary,
            'coin' => ceil(floatval($book_data->price)),
        );
        $id = $this->book_model->insertBook($data);
        
        //echo "http://".$_SERVER["HTTP_HOST"]."/books/id/".$id;exit;
        redirect("http://".$_SERVER["HTTP_HOST"]."/books?id=".$id);
    }
} 
?>